from django.shortcuts import render

from rest_framework import  viewsets
from .models import  Student,Classes
from .serializers import StudentSerializer,ClassesSerializer
from rest_framework.response import Response
from rest_framework.decorators import action
# Create your views here.
from rest_framework.status import HTTP_201_CREATED,HTTP_404_NOT_FOUND,HTTP_204_NO_CONTENT


# 封装的形式
# 继承rest_framework的 ViewSet 实现成为 视图集
# class StudentViewSet(viewsets.ViewSet):
#
#     # 查看所有学生
#     def list(self, request):
#         # 获取所有的学生信息
#         students = Student.objects.filter(is_delete=False).all()  # 查询集 [(模型对象),(模型对象)]
#         # 通过序列化器吧 queryset转换成 json格式数据  序列化
#         # 吧要转换的数据 传入 序列化器作为参数  如果是多个学生 many = True
#         serialize = StudentSerializer(students, many=True)
#         # serialize.data  里面保存的就是json格式数据
#         # 通过rest_framework通过的Response 返回 json格式数据
#         return Response(serialize.data)
#
#     # 添加学生
#     def create(self, request):
#         # 从请求里面得到学生数据 （json） request.data
#         # 通过序列化器进行 反序列化
#         #  serialize==>实例对象
#         serialize = StudentSerializer(data = request.data)
#         # 数据校验 is_valid  主动报错
#         serialize.is_valid(raise_exception=True)
#         # 数据的持久化保存  validated_data拿到前端传入的数据
#         serialize.save()
#         # 返回响应
#         # serialize.data 创建的学生模型对象的json数据
#         return Response(serialize.data,status=HTTP_201_CREATED)
#
#     # 查看指定学生
#     def retrieve(self, request, pk):
#         # 获取指定学生数据
#         try:
#             # 模型对象
#             student = Student.objects.filter(is_delete=False).get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#         # 进行序列化器  单条数据  many=False
#         serialize = StudentSerializer(student)
#         # 返回响应
#         return Response(serialize.data)
#
#     def update(self, request, pk):
#         # 获取指定学生数据
#         try:
#             # 模型对象  student 旧数据
#             student = Student.objects.filter(is_delete=False).get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         # 获取前端传入的json格式数据  新数据 request.data
#         # 进行 反序列化
#         serialize = StudentSerializer(instance=student, data=request.data)
#         # 数据校验
#         serialize.is_valid(raise_exception=True)
#         # 保存
#         serialize.save()
#
#         return Response(serialize.data, status=HTTP_201_CREATED)
#
#
#     def destroy(self, request, pk):
#         try:
#             # 模型对象  student 旧数据
#             student = Student.objects.filter(is_delete=False).get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=HTTP_404_NOT_FOUND)
#
#         # 调用删除方法  ==》 调用修改方法
#         student.delete()
#         # 返回状态码
#         return Response(status=HTTP_204_NO_CONTENT)

# 模型视图集
# 学生视图集
class StudentViewSet(viewsets.ModelViewSet):
    # 指定 查询数据  变量名必须是 queryset
    queryset = Student.objects.filter(is_delete=False)
    # 指定 序列化器 变量名必须是serializer_class
    serializer_class = StudentSerializer


class ClassesViewSet(viewsets.ModelViewSet):
    # 指定操作的模型
    queryset = Classes.objects.filter(is_delete=False)

    # 指定 序列化器
    serializer_class = ClassesSerializer

    # 查看最新的班级 get
    # 127.0.0.1:8000/drfstudy/classes/last_classes
    # False 就是不带参数
    @action(methods=["get"],detail= False)
    def last_classes(self,request):
        #  模型对象
        # last_classes = Classes.objects.filter(is_delete=False).last()
        # self.get_queryset() == Classes.objects.filter(is_delete=False)
        last_classes = self.get_queryset().last()
        # 序列化
        # serializer = ClassesSerializer(last_classes)
        # get_serializer(last_classes) =  ClassesSerializer(last_classes)
        serializer = self.get_serializer(last_classes)
        # 响应数据
        return Response(serializer.data)

